package v3.object.command.list;

import galaxy.obj.Point;

import java.util.ArrayList;
import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.Screen;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.OnScreen;
import v3.object.ship.Ship;
import v3.object.space.Sector;
import v3.object.space.Universe;
import v3.object.space.iSpaceObj;

public class Analysis extends aCommand
{
	int x = 0;
	int y = 0;
	Point analysisPoint;
	private double distance;

	public Analysis()
	{
		super();
		commandName = "ANALYSIS";
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{

//		distance = ship.getPosition().distance(analysisPoint);
//		double successfullAnalysisProb = Math.pow(ship.getEfficenceAnalysys(), distance);
//		double rnd = cfg.rnd.nextDouble();
//		if (rnd < successfullAnalysisProb)
//			return true;
//
//		iCommand c = new ComputerMessage("SUBSPACE INTERFERENCE PREVENT A SUCCESSFUL ANALYSYS "+rnd+" < " +successfullAnalysisProb);
//		c.execute(cfg, universe, ship, commands, input);
		
		return true;

	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		double e = ship.getEnergyConsume((this.getClass()));
		return e * distance;
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		x = Integer.parseInt(input[1]);
		y = Integer.parseInt(input[2]);
		analysisPoint = new Point(x, y);
	}

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		ShortSensor ss = new ShortSensor();
		Screen screen = ss.getShortSensorScreen(ship.getPosition(), ship.getSSsize(), universe, ship.getPosition());
		
		Sector s = universe.getSector(analysisPoint);		
		ArrayList<iSpaceObj> list = s.getList();
		
		StringBuilder sb = new StringBuilder(screen.toString());

		sb.append("\n");
		sb.append("ANALYSIS SECTOR ");
		sb.append(analysisPoint.toString());
		sb.append("\n");
		
		int index = 1 ;
		if(list==null)
		{
			sb.append("SECTOR EMPTY ");
		}
		else
		{
			for(iSpaceObj o : list)
			{
				
				sb.append("OBJECT: ");
				sb.append(index);
				sb.append("\n");
				sb.append(o.getDescription());

				sb.append("\n---------------\n");
				index++;
			}
			sb.append("END ANALYSYS\n");
		}
		
		
		iCommand c = new OnScreen(commandName, sb.toString());
		c.execute(cfg, universe, ship, commands, input);
		return true;
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return ship.getTimeConsume((this.getClass()));
	}

	@Override
	public void getHelp(Ship ship)
	{
		String help = "Analysis (x,y) [an]\n\n" + "Give complete information of the content of the sector (x,y)" + "Time Cost "+ship.getTimeConsume((this.getClass()))+"\n" + "Energy Cost " +ship.getEnergyConsume((this.getClass()))+" times distance";
		help = TextUtil.formatHelp(help);
		System.out.println(help);
	}

	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		return true;
	}
}
